package com.comp.controller;

import com.comp.pojo.Addressbook;
import com.comp.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    // 通讯录列表
    @RequestMapping("/list")
    public String list(Model model) {
        List list = addressBookService.queryList();
        model.addAttribute("list",list);
        return "/address/list";
    }

    // 通讯录添加
    @RequestMapping("/add")
    public String add() {
        return "/address/add";
    }

    // 通讯录添加-提交
    @RequestMapping("/add_submit")
    public String add_submit(Model model,Addressbook addressbook) {
        int result = addressBookService.insertAddress(addressbook);
        model.addAttribute("result",result);
        return "/address/add_submit";
    }

    // 通讯录修改
    @RequestMapping("/edit")
    public String edit(Model model,Integer id) {
        Addressbook addressbook = addressBookService.queryById(id);
        model.addAttribute("addressbook",addressbook);
        return "/address/edit";
    }

    // 通讯录修改-提交
    @RequestMapping("/edit_submit")
    public String edit_submit(Model model,Addressbook addressbook) {
        int result = addressBookService.updateAddress(addressbook);
        model.addAttribute("result",result);
        return "/address/edit_submit";
    }

    // 通讯录删除
    @RequestMapping("/del")
    public String del(Model model,Integer id) {
        int result = addressBookService.deleteAddress(id);
        model.addAttribute("result",result);
        return "/address/del";
    }
}
